/**
 * @Title: BaseController.java
 * @Package com.qding.smart.common.web.controller
 * @Description: TODO
 * Copyright: Copyright (c) 2011 
 * Company:成都康赛电子科大信息技术有限责任公司
 * 
 * @author Comsys-Administrator
 * @date 2016年6月19日 下午4:15:59
 * @version V1.0
 */

package com.qding.park.manager.rest.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;

import com.qding.park.common.core.constant.CacheKeyThree;
import com.qding.park.common.core.exceptions.BaseException;
import com.qding.park.common.core.exceptions.ExceptionMessage;
import com.qding.park.common.core.exceptions.IExceptionHandle;
import com.qding.park.common.core.exceptions.auth.AuthException;
import com.qding.park.common.core.exceptions.business.validator.RchlValidatorException;
import com.qding.park.common.core.redis.JedisClient;
import com.qding.park.common.core.redis.SerializeUtils;
import com.qding.park.common.utils.ListUtil;
import com.qding.park.common.utils.string.StringUtil;
import com.qding.park.common.web.controller.BaseController;
import com.qding.park.common.web.session.SessionInfo;
import com.qding.park.manager.pojo.PowerInfo;
import com.qding.park.manager.pojo.enums.OrderServiceType;
import com.qding.park.manager.rest.conf.SystemConfig;
import com.qding.smart.sso.client.entry.UserInfo;
import com.qding.smart.sso.client.util.SSOClientHelp;

/**
 * @ClassName: BaseController
 * @Description: TODO
 * @author Comsys-Administrator
 * @date 2016年6月19日 下午4:15:59
 *
 */
public abstract class WebBaseController extends BaseController {

	@Autowired
	protected SystemConfig systemConfig;
	@Autowired
	protected JedisClient jedisClient;
	
	private static  final String  REQUEST_VERSION_HEADER_NAME="verser";
	
	/**
	 * @Description (TODO这里用一句话描述这个方法的作用)
	 * @return
	 */
	protected String getVersion() {
	    
	    return request.getHeader(REQUEST_VERSION_HEADER_NAME);
      
    }

	protected void saveSessionInfo(String userId, SessionInfo session) {

		String session_key = CacheKeyThree.REST_SESSION_KEY + ":" + userId;
		jedisClient.set(session_key, SerializeUtils.serialize(session), 60 * 60 * 24);
	}

	protected void clearSessionInfo(String userId) {

		String session_key = CacheKeyThree.REST_SESSION_KEY + ":" + userId;
		jedisClient.del(session_key);
	}

	protected SessionInfo getSessionInfo(String userId) {

		String session_key = CacheKeyThree.REST_SESSION_KEY + ":" + userId;
		SessionInfo sessioninfo = (SessionInfo) jedisClient.get(session_key, SessionInfo.class);
		return sessioninfo;
	}

	protected boolean isDemoRequestByOrder(String orderId) {

		if (StringUtil.isBlank(orderId)) {
			return false;
		}

		if (!orderId.endsWith(OrderServiceType.DEMO_ORDER.getTag())) {
			return false;
		}

		return true;
	}

	protected boolean isDemoRequestByparkid(String parkId) {

		List<String> parkids = systemConfig.calculationDemoPark();
		if (!parkids.contains(parkId)) {
			return false;
		}
		return true;
	}

	protected boolean isDemoRequestByProject(String project) {

		List<String> projects = systemConfig.calculationDemoProject();
		if (!projects.contains(project)) {
			return false;
		}
		return true;
	}

	/**
	 * @return 判断是否是演示请求
	 */
	protected boolean isDemoRequest(String userId) {

		String session_key = CacheKeyThree.REST_SESSION_KEY + ":" + userId;
		SessionInfo session = (SessionInfo) jedisClient.get(session_key, SessionInfo.class);
		if (session == null) {
			return false;
		}
		String project = session.getProjectId();
		List<Integer> parklist = session.getParklist();

		if (StringUtil.isBlank(project)) {
			return false;
		}

		if (!systemConfig.calculationDemoProject().contains(project)) {
			return false;
		}

		if (!ListUtil.isEmpty(parklist)) {
			for (int parkid : parklist) {
				if (!systemConfig.calculationDemoPark().contains(parkid)) {
					return false;
				}
			}
		}
		return true;
	}

}
